package com.clearliang.wxredpacket.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.clearliang.wxredpacket.R;


/**
 * Created by ClearLiang on 2018/12/27
 * <p>
 * Function :
 */
public class LoginActivity extends AppCompatActivity {

    private LineTextView ltvUsername, ltvPassword;
    private EditText etUsername, etPassword;
    private Button btnLogin;
    private CheckBox cbChangeToken;

    private final String USERNAME = "ruxikeji";
    private final String USERNAME1 = "ClearLiang";
    private final String PASSWORD = "2019";
    private boolean isRemember = true;
    private String mUsername,mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvent();

        isRemember = SPUtils.getInstance("Login").getBoolean("isRemember",true);
        if(isRemember){
            etUsername.setText(SPUtils.getInstance("Login").getString("UserName"));
            etPassword.setText(SPUtils.getInstance("Login").getString("PassWord"));
        }
    }

    private void initEvent() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkInput()) {
                    login();
                }
            }
        });
        cbChangeToken.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isRemember = b;
            }
        });
    }

    private boolean checkInput() {
        if (TextUtils.isEmpty(etUsername.getText().toString().trim())) {
            showToast("用户名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
            showToast("密码不能为空");
            return false;
        }
        return true;
    }

    private boolean login() {
        mUsername = etUsername.getText().toString().trim();
        mPassword = etPassword.getText().toString().trim();
        if (USERNAME.equals(mUsername) || USERNAME1.equals(mUsername)) {
            if (PASSWORD.equals(mPassword)) {
                if(isRemember){
                    SPUtils.getInstance("Login").put("UserName",mUsername);
                    SPUtils.getInstance("Login").put("PassWord",mPassword);
                    SPUtils.getInstance("Login").put("isRemember",isRemember);
                }
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            } else {
                showToast("密码不正确");
            }
        } else {
            showToast("用户名不正确");
        }
        return true;
    }

    private void initView() {

        ltvUsername = (LineTextView) findViewById(R.id.ltv_username);
        etUsername = (EditText) findViewById(R.id.et_username);
        ltvPassword = (LineTextView) findViewById(R.id.ltv_password);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);

        ltvUsername.setText("用户名");
        ltvUsername.setTextSize(18);
        ltvPassword.setText("密码");
        ltvPassword.setTextSize(18);
        cbChangeToken = (CheckBox) findViewById(R.id.cb_change_token);
    }

    private void showToast(String msg) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.showShort(msg);
    }
}
